package com.goldcn.user.controller;

import com.goldcn.user.service.CodeService;
import com.goldcn.user.service.RedisService;
import com.google.code.kaptcha.impl.DefaultKaptcha;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import net.sf.json.JSONObject;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.imageio.ImageIO;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.awt.image.BufferedImage;

/**
 * Created by Administrator on 2016/7/21.
 */
@Api(value = "图形验证码")
@RestController
public class CodeController {
    private int width = 90;//定义图片的width
    private int height = 20;//定义图片的height
    private int codeCount = 4;//定义图片上显示验证码的个数
    private int xx = 15;
    private int fontHeight = 18;
    private int codeY = 16;
    char[] codeSequence = { 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J',
            'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W',
            'X', 'Y', 'Z', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9' };

    private Logger logger = Logger.getLogger(CodeController.class);

    @Autowired
    RedisService redisService;

    @Autowired
    CodeService codeService;

    @Autowired
    DefaultKaptcha captchaProducer;


    @ApiOperation(value = "获取验证码")
    @RequestMapping(value = "/sendCode/{id}",method = RequestMethod.GET)
    public void getCodes(@PathVariable String id, HttpServletRequest req, HttpServletResponse resp) throws Exception{
        redisService.addOrUpdateRAS(id, id);
        resp.setDateHeader("Expires", 0);
        resp.setHeader("Cache-Control",
                "no-store, no-cache, must-revalidate");
        resp.addHeader("Cache-Control", "post-check=0, pre-check=0");
        resp.setHeader("Pragma", "no-cache");
        resp.setContentType("image/jpeg");

        String capText = captchaProducer.createText();
        try {
            redisService.addOrUpdateRAS(capText.toString().toLowerCase(),capText.toString().toLowerCase());
        } catch (Exception e) {
            e.printStackTrace();
        }

        BufferedImage bi = captchaProducer.createImage(capText);
        ServletOutputStream out = resp.getOutputStream();
        ImageIO.write(bi, "jpg", out);
        try {
            out.flush();
        } finally {
            out.close();
        }
    }

    /***
     * 判断验证码
     * @param id
     * @param req
     * @param resp
     * @return
     */
    @ApiOperation(value = "验证验证码")
    @RequestMapping(value = "/verificationCode/{id}/{code}",method = RequestMethod.GET)
    public Object verificationCode(@PathVariable String id,@PathVariable String code, HttpServletRequest req, HttpServletResponse resp){
        return codeService.verificationCode(id,code);
    }


    /***
     * 判断验证码1
     * @param
     * @param req
     * @param resp
     * @return
     */
    @ApiOperation(value = "验证验证码")
    @RequestMapping(value = "/verificationCode",method = RequestMethod.POST)
    public Object verificationCode1(@RequestBody JSONObject json, HttpServletRequest req, HttpServletResponse resp){
        return codeService.verificationCode1(json);
    }

    /**
     * 缓存id和验证码
     */
    @ApiOperation(value = "缓存id和验证码")
    @RequestMapping(value = "/saveidOrCode/{id}/{code}",method = RequestMethod.GET)
    public Object saveidOrCode(@PathVariable String id,@PathVariable String code, HttpServletRequest req, HttpServletResponse resp){
        redisService.addOrUpdateRAS(id, id);
        redisService.addOrUpdateRAS(code,code);
        return "1";
    }
}
